<template>
  <div class="app-container">
    <h5 class="titleInfo">菜单新增</h5>
    <el-form class="form" ref="form" :rules="rules" :model="form" label-width="130px">
      <el-form-item label="菜单名称" prop="menuName">
        <el-input v-model="form.menuName"></el-input>
      </el-form-item>
      <el-form-item label="所属系统">
        <Fromplatfrom @childfun="childfun"></Fromplatfrom>
      </el-form-item>
      <el-form-item label>
        <p style="margin: 0;color: #a00303;">请先选择所属系统再选择上级菜单</p>
      </el-form-item>

      <el-form-item label="上级菜单" prop="parentId">
        <!-- <el-input v-model="form.parentId"></el-input> -->
        <!-- <el-select v-model="form.parentId" filterable placeholder="请选择">
          <el-option
            v-for="item in parentOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>id
        </el-select>-->
        <el-tree
          style="margin-top:6px"
          :data="treeData"
          ref="treeForm"
          check-strictly
          show-checkbox
          node-key="name"
          :props="defaultProps"
          @check-change="handleClick"
        >
          <!-- <span class="custom-tree-node" slot-scope="{ node, data }">
            <span>{{ node.label }}</span>
            <span>
              <el-button type="text" size="mini" @click="() => append(data)">Append</el-button>
              <el-button type="text" size="mini" @click="() => remove(node, data)">Delete</el-button>
            </span>
          </span>-->
        </el-tree>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择">
          <el-option
            v-for="item in statusOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜单描述" prop="description">
        <el-input v-model="form.description"></el-input>
      </el-form-item>
      <!-- <el-form-item label="权限字段" prop="code">
                <el-input v-model="form.code"></el-input>
      </el-form-item>-->
      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort"></el-input>
      </el-form-item>
      <el-form-item label="图标">
        <icons style="width:236px" :defaultIcon="form.icon" @IconChoice="getIcon"></icons>
      </el-form-item>
      <el-form-item label="按钮菜单">
        <el-select v-model="form.type" filterable placeholder="请选择">
          <el-option label="菜单" :value="0"></el-option>
          <el-option label="按钮" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="url" prop="url">
        <el-input v-model="form.url"></el-input>
         <p style="margin: 0;color: #a00303;">不要加/直接name</p>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitFn('form')">立即提交</el-button>
      </el-form-item>
    </el-form>
    <!-- <el-button @click="clearfn">清空</el-button> -->
  </div>
</template>
<script>
import icons from "../../icons/index";

import { listAllPermission } from "@/api/roleManage";
import { listAdminMenus, saveMenu } from "@/api/menuManage";
import Fromplatfrom from "../components/platform";
export default {
  name: "menuManageAdd",

  data() {
    return {
      defaultProps: {
        children: "children",
        label: "name"
      },
      treeData: [],
      fileNum: 1,
      parentOption: [
        {
          value: 0,
          label: "顶级"
        }
      ],
      statusOption: [
        {
          value: 0,
          label: "开启"
        },
        {
          value: 1,
          label: "关闭"
        }
      ],
      form: {
        icon: "",
        subSys: -1,
        parentId:0,
      },
      rules: {
        menuName: [
          { required: true, message: "请输入菜单名称", trigger: "blur" }
        ],
        // parentId: [
        //   { required: true, message: "请选择上级菜单", trigger: "blur" }
        // ],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        code: [{ required: true, message: "请输入权限字段", trigger: "blur" }],
        url: [{ required: true, message: "请输入url", trigger: "blur" }],
        plat: [
          {
            required: true,
            message: "请输入系统"
          }
        ]
      }
    };
  },
  inject: ["reload"],
  mounted() {
    // this.getMenu();
    // console.log(this.$store.getters['permission_routes'])
  },
  components: { icons, Fromplatfrom },
  methods: {
    handleClick(data, checked, node) {
   
      if (this.$refs["treeForm"].getCheckedNodes().length > 1) {
        if (checked) {
     
          this.$refs.treeForm.setCheckedNodes([]);
          this.$refs.treeForm.setCheckedNodes([data]);
          //交叉点击节点
        } else {
          this.$refs.treeForm.setCheckedNodes([]);
          //点击已经选中的节点，置空
        }
      }
      this.form.parentId = this.$refs["treeForm"].getCheckedNodes()[0]
        ? this.$refs["treeForm"].getCheckedNodes()[0].menuId? this.$refs["treeForm"].getCheckedNodes()[0].menuId:0
        : 0;
    },
    getMenu() {
      let datas = {
        pageNum: 1,
        pageSize: 10000,
        subSys: this.form.subSys
      };
      listAllPermission(datas)
        .then(res => {
          if (res.data.code == 0) {
            console.log(res.data.data);
          if( res.data.data.length)  res.data.data.unshift({
            id:0,
            name:'顶级'
          })
            this.treeData = res.data.data;
            // let resData = res.data.data.menuDTOS;
            // resData.forEach(e => {
            //   this.parentOption.push({
            //     value: Number(e.id),
            //     label: String(e.menuName)
            //   });
            // });
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    getIcon(data) {
      this.form.icon = data.value;
    },
    childfun(val) {
      console.log(val);
      this.form.subSys = val === "" ? -1 : val;
      this.getMenu();
    },
    // 立即提交
    submitFn(e) {
      // console.log(this.$refs["treeForm"].getCheckedNodes(), this.form);
      //   this.form.parentId =this.$refs["treeForm"].getCheckedNodes()[0].id
      if (this.form.subSys == -1) {
        this.$message({
          showClose: true,
          message: "系统必须选择",
          type: "warning"
        });
        return;
      }
      this.$refs[e].validate(valid => {
        if (valid) {
          if (this.form.icon == "无图标") {
            this.form.icon = null;
          }
          let datas = this.form;
          saveMenu(datas)
            .then(res => {
              console.log(res);
              if (!res.data.code) {
                this.$message({
                  showClose: true,
                  message: "添加成功",
                  type: "success"
                });
                this.$router.push("/menuManageIndex");
                this.reload();
              } else {
                this.$message({
                  showClose: true,
                  message: res.data.msg,
                  type: "warning"
                });
              }
            })
            .catch(error => {
              this.$message.error(error);
            });
        } else {
          this.$message({
            message: "必填项不能为空",
            type: "warning"
          });
        }
      });
    }
  }
};
</script>
<style scoped>
.app-container {
  padding: 0.1px !important;
}
</style>